// ============================== 导入依赖 ============================== //

import API from './API'
import TRANS from './TRANS'

// ============================== 导出初始化工具 ============================== //

export default {
  /**
   * 初始化内核
   */
  async initCORE() {
    // 主题初始化
    this.themeInit()

    // TOKEN 为空，不再继续
    if (!sessionStorage.TOKEN) return

    // 开启遮罩
    this.loading()

    // 设置鉴权
    this.GRPC.META.authorization = `Bearer ${sessionStorage.TOKEN}`

    // 多项查询
    const multi = [
      API.getUser(),
      API.getOrgan(),
      API.getDistrict(),
    ]
    const [USER, ORGAN, DISTRICT] = await Promise.all(multi)

    // 设置用户
    this.USER = USER

    // 设置菜单
    const MENU = USER.menuTree
    this.MENU.TREE = TRANS.transTree(MENU)
    this.MENU.MAP = TRANS.transTreeMap({}, MENU)
    this.MENU.DICT = TRANS.transTreeDict([], MENU)
    this.MENU.NAME = TRANS.transAllName({}, this.MENU.MAP)
    this.MENU.ROUTE = TRANS.transMenuRoute({}, MENU)

    // 设置组织
    this.ORGAN.TREE = TRANS.transTree(ORGAN)
    this.ORGAN.MAP = TRANS.transTreeMap({}, ORGAN)
    this.ORGAN.DICT = TRANS.transTreeDict([], ORGAN)
    this.ORGAN.NAME = TRANS.transAllName({}, this.ORGAN.MAP)
    this.ORGAN.MY_TREE = TRANS.transMyTree(this.ORGAN.TREE, USER.organId)
    this.ORGAN.MY_MAP = TRANS.transTreeMap({}, this.ORGAN.MY_TREE)
    this.ORGAN.MY_DICT = TRANS.transTreeDict([], this.ORGAN.MY_TREE)

    // 设置省市区
    this.DISTRICT.TREE = DISTRICT.TREE
    this.DISTRICT.MAP = DISTRICT.MAP
    this.DISTRICT.DICT = DISTRICT.DICT
    this.DISTRICT.PROVINCE = DISTRICT.PROVINCE
    this.DISTRICT.CITY = DISTRICT.CITY
    this.DISTRICT.AREA = DISTRICT.AREA

    // 关闭遮罩
    this.unloading()
  },
}
